home *** CD-ROM | disk | FTP | other *** search
/ Creative Computers / Creative Computers CD-ROM, Volume 1 (Legendary Design Technologies, Inc.)(1994).iso / shareware / games / uchess / src / util.c < prev    next >
C/C++ Source or Header  |  1994-11-17  |  11KB  |  515 lines

  1. //#define ZEROCACHE 1
  2. /*
  3.  * util.c - C source for GNU CHESS
  4.  *
  5.  * Copyright (c) 1988,1989,1990 John Stanback
  6.  * Copyright (c) 1992 Free Software Foundation
  7.  *
  8.  * This file is part of GNU CHESS.
  9.  *
  10.  * GNU Chess is free software; you can redistribute it and/or modify
  11.  * it under the terms of the GNU General Public License as published by
  12.  * the Free Software Foundation; either version 2, or (at your option)
  13.  * any later version.
  14.  *
  15.  * GNU Chess is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  * GNU General Public License for more details.
  19.  *
  20.  * You should have received a copy of the GNU General Public License
  21.  * along with GNU Chess; see the file COPYING.  If not, write to
  22.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  */
  24. #include "gnuchess.h"
  25. unsigned int __aligned TTadd = 1;
  26. short int __aligned recycle;
  27. short int __aligned ISZERO = 1;
  28. extern char mvstr[4][6];
  29. #ifdef CACHE
  30. extern struct etable __far __aligned etab[2][ETABLE];
  31. #endif
  32.  
  33. int
  34. parse (FILE * fd, short unsigned int *mv, short int side, char *opening)
  35. {
  36.   register int c, i, r1, r2, c1, c2;
  37.   char s[128];
  38.   char *p;
  39.  
  40.   while ((c = getc (fd)) == ' ' || c == '\n') ;
  41.   i = 0;
  42.   s[0] = (char) c;
  43.   if (c == '!')
  44.     {
  45.       p = opening;
  46.       do
  47.     {
  48.       *p++ = c;
  49.       c = getc (fd);
  50.       if (c == '\n' || c == EOF)
  51.         {
  52.           *p = '\0';
  53.           return 0;
  54.         }
  55.       } while (true);
  56.     }
  57.   while (c != '?' && c != ' ' && c != '\t' && c != '\n' && c != EOF)
  58.     s[++i] = (char) (c = getc (fd));
  59.   s[++i] = '\0';
  60.   if (c == EOF)
  61.     return (-1);
  62.   if (s[0] == '!' || s[0] == ';' || i < 3)
  63.     {
  64.       while (c != '\n' && c != EOF)
  65.     c = getc (fd);
  66.       return (0);
  67.     }
  68.   if (s[4] == 'o')
  69.     *mv = ((side == black) ? LONGBLACKCASTLE : LONGWHITECASTLE);
  70.   else if (s[0] == 'o')
  71.     *mv = ((side == black) ? BLACKCASTLE : WHITECASTLE);
  72.   else
  73.     {
  74.       c1 = s[0] - 'a';
  75.       r1 = s[1] - '1';
  76.       c2 = s[2] - 'a';
  77.       r2 = s[3] - '1';
  78.       *mv = (locn (r1, c1) << 8) | locn (r2, c2);
  79.     }
  80.   if (c == '?')
  81.     {                /* Bad move, not for the program to play */
  82.       *mv |= 0x8000;        /* Flag it ! */
  83.       c = getc (fd);
  84.     }
  85.   return (1);
  86. }
  87.  
  88.  
  89. #if ttblsz
  90.  
  91. #define CB(i) (unsigned char) ((color[2 * (i)] ? 0x80 : 0)\
  92.            | (board[2 * (i)] << 4)\
  93.            | (color[2 * (i) + 1] ? 0x8 : 0)\
  94.            | (board[2 * (i) + 1]))
  95.  
  96. int
  97. ProbeTTable (short int side,
  98.          short int depth,
  99.          short int ply,
  100.          short int *alpha,
  101.          short int *beta,
  102.          short int *score)
  103.  
  104. /*
  105.  * Look for the current board position in the transposition table.
  106.  */
  107.  
  108. {
  109.   register struct hashentry *ptbl;
  110.   register /*unsigned*/ short i= 0;  /*to match new type of rehash --tpm*/
  111.  
  112.   ptbl = &ttable[side][hashkey % (ttblsize)];
  113.  
  114.   while (true)
  115.     {
  116.       if (ptbl->depth == 0)
  117.     return false;
  118.       if (ptbl->hashbd == hashbd)
  119.     break;
  120.       if (++i > rehash)
  121.     return false;
  122.       ptbl++;
  123.     }
  124.  
  125.   /* rehash max rehash times */
  126.   PV = SwagHt = ptbl->mv; // this is out of loop, in loop was wierd
  127.   if ((ptbl->depth >= (short) depth))
  128.     {
  129. #ifdef HASHTEST
  130.       for (i = 0; i < 32; i++)
  131.     {
  132.       if (ptbl->bd[i] != CB (i))
  133.         {
  134. #ifndef BAREBONES
  135.           HashCol++;
  136.           ShowMessage (CP[199]);    /*ttable collision detected*/
  137. #endif
  138.           break;
  139.         }
  140.     }
  141. #endif /* HASHTEST */
  142.  
  143.  
  144. //      PV = SwagHt = ptbl->mv; // was in loop in 4PL64 moved out for better perf
  145. #ifndef BAREBONES
  146.       HashCnt++;
  147. #endif
  148.       if (ptbl->flags & truescore)
  149.     {
  150.       *score = ptbl->score;
  151.       /* adjust *score so moves to mate is from root */
  152.       if (*score > 9000)
  153.         *score -= ply;
  154.       else if (*score < -9000)
  155.         *score += ply;
  156.       *beta = -20000;
  157.     }
  158.       else if (ptbl->flags & lowerbound)
  159.     {
  160.       if (ptbl->score > *alpha)
  161.         *alpha = ptbl->score; //  - 1;  FIX by K. Sian, was ptbl->score - 1
  162.     }
  163.       return (true);
  164.     }
  165.   return (false);
  166. }
  167.  
  168.  
  169.  
  170. #ifndef V4PL66
  171. int
  172. PutInTTable (short side,
  173.              short score,
  174.              short depth,
  175.              short ply,
  176.              short alpha,
  177.              short beta,
  178.              unsigned short mv)
  179.  
  180. /*
  181.  * Store the current board position in the transposition table.
  182.  */
  183.  
  184. {
  185.   register struct hashentry *ptbl;
  186.   register /*unsigned*/ short i = 0;    /*to match new type of rehash --tpm*/
  187.  
  188.   ptbl = &ttable[side][hashkey % ttblsize];
  189.   while (true)
  190.     {
  191.       if (ptbl->depth == 0 || ptbl->hashbd == hashbd)
  192.         break;
  193.       if (++i > rehash)
  194.         {
  195. #ifndef BAREBONES
  196.           THashCol++;
  197. #endif
  198.           ptbl += recycle;
  199.           break;
  200.         }
  201.       ptbl++;
  202.     }
  203.  
  204.   TTadd++;
  205. #ifndef BAREBONES
  206.   HashAdd++;
  207. #endif
  208.   ptbl->hashbd = hashbd;
  209.   ptbl->depth = (unsigned char) depth;
  210.   ptbl->mv = mv;
  211. #ifdef DEBUG
  212.   if (debuglevel & 32)
  213.     {
  214.       algbr (mv >> 8, mv & 0xff, 0);
  215.       printf ("-add-> h=%lx d=%d s=%d p=%d a=%d b=%d %s\n", hashbd, depth,
  216.                  score , ply, alpha, beta, mvstr);
  217.     }
  218. #endif
  219.   if (score > beta)
  220.     {
  221.       ptbl->flags = lowerbound;
  222.       ptbl->score = beta + 1;
  223.     }
  224.   else
  225.     {
  226.       ptbl->flags = truescore;
  227.       /* adjust score so moves to mate is from this ply */
  228.       if (score > 9000)
  229.         score += ply;
  230.       else if (score < -9000)
  231.         score -= ply;
  232.       ptbl->score = score;
  233.     }
  234.  
  235. #ifdef HASHTEST
  236.   for (i = 0; i < 32; i++)
  237.     {
  238.       ptbl->bd[i] = CB (i);
  239.     }
  240. #endif /* HASHTEST */
  241.   return true;
  242. }
  243.  
  244. #else
  245. int
  246. PutInTTable (short int side,
  247.          short int score,
  248.          short int depth,
  249.          short int ply,
  250.          short int alpha,
  251.          short int beta,
  252.          short unsigned int mv)
  253.  
  254. /*
  255.  * Store the current board position in the transposition table.
  256.  */
  257.  
  258. {
  259.   register struct hashentry *ptbl;
  260.   register /*unsigned*/ short i= 0;  /*to match new type of rehash --tpm*/
  261.  
  262.   ptbl = &ttable[side][hashkey % (ttblsize)];
  263.  
  264.   while (true)
  265.     {
  266. //      if (ptbl->depth == 0 || ptbl->hashbd == hashbd)
  267. //    break;
  268.        if (ptbl->depth == 0) break;
  269.        else if(ptbl->hashbd == hashbd  && ptbl->depth > depth && abs(score) <9000) 
  270.          return false;
  271.        else if (ptbl->hashbd == hashbd) break;
  272.       if (++i > rehash)
  273.     {
  274. #ifndef BAREBONES
  275.       THashCol++;
  276. #endif
  277.       ptbl += recycle;
  278.       break;
  279.     }
  280.       ptbl++;
  281.     }
  282.  
  283. #ifndef BAREBONES
  284.   TTadd++;
  285.   HashAdd++;
  286. #endif
  287.   /* adjust score so moves to mate is from this ply */
  288.   if (score > 9000)
  289.     score += ply;
  290.   else if (score < -9000)
  291.     score -= ply;
  292.   ptbl->hashbd = hashbd;
  293.   ptbl->depth = (unsigned char) depth;
  294.   ptbl->score = score;
  295.   ptbl->mv = mv;
  296. #ifdef DEBUG4
  297.   if (debuglevel & 32)
  298.     {
  299.       algbr (mv >> 8, mv & 0xff, 0);
  300.       printf ("-add-> h=%lx d=%d s=%d p=%d a=%d b=%d %s\n", hashbd, depth, score, ply, alpha, beta, mvstr);
  301.     }
  302. #endif
  303.   if (score > beta)
  304.     {
  305.       ptbl->flags = lowerbound;
  306.       ptbl->score = beta + 1;
  307.     }
  308.   else
  309.     ptbl->flags = truescore;
  310.  
  311. #ifdef HASHTEST
  312.   for (i = 0; i < 32; i++)
  313.     {
  314.       ptbl->bd[i] = CB (i);
  315.     }
  316. #endif /* HASHTEST */
  317.   return true;
  318. }
  319. #endif // 4pl66
  320.  
  321.    
  322. // static struct hashentry *ttagew, *ttageb;
  323.  
  324. void
  325. ZeroTTable (void)
  326. {
  327. //   register struct hashentry *w, *b;
  328.  
  329. /* I am adding these 2 memsets! */
  330.  if (!TTadd)
  331.   return;
  332. #ifndef AMIGA
  333.   memset((char *)ttable[0],0,sizeof(struct hashentry)*(ttblsize+rehash));
  334.   memset((char *)ttable[1],0,sizeof(struct hashentry)*(ttblsize+rehash));
  335. #else
  336.   ClearMem(ttable[0],sizeof(struct hashentry)*(ttblsize+rehash));
  337.   ClearMem(ttable[1],sizeof(struct hashentry)*(ttblsize+rehash));
  338. #endif
  339. #ifdef ZEROCACHE
  340. #ifdef CACHE
  341. #ifndef AMIGA
  342.    memset ((char *) etab, 0, sizeof (etab));
  343. #else
  344.    ClearMem(etab,sizeof(etab));
  345. #endif // amiga
  346. #endif // cache
  347. #endif // zerocache
  348.     TTadd = 0; 
  349. }
  350.  
  351. #ifdef HASHFILE
  352. int Fbdcmp(char *a,char *b)
  353. {
  354.     register int i;
  355.     for(i = 0;i<32;i++)
  356.         if(a[i] != b[i])return false;
  357.     return true;
  358. }
  359. int
  360. ProbeFTable (short int side,
  361.          short int depth,
  362.          short int ply,
  363.          short int *alpha,
  364.          short int *beta,
  365.          short int *score)
  366.  
  367. /*
  368.  * Look for the current board position in the persistent transposition table.
  369.  */
  370.  
  371. {
  372.   register short int i;
  373.   register unsigned long hashix;
  374.   struct fileentry new, t;
  375.  
  376.   hashix = ((side == white) ? (hashkey & 0xFFFFFFFE) : (hashkey |